﻿#region Usings
using System;
using System.Collections.Generic;

using BK.Model;
using BK.Repositories.Abstract;
using BK.Repositories.EF;
#endregion

namespace BK.Repositories.Factory
{
//    public static class RepositoryFactory
//    {
//        #region Readonly & Static Fields
//        [ThreadStatic]
//        private static BkDataContext _bkDataContext;
//
//        [ThreadStatic]
//        private static Dictionary<Type, Type> _implementationMapping;
//
//        [ThreadStatic]
//        private static Dictionary<Type, IRepository> _repositories;
//        #endregion
//
//        #region Class Properties
//        public static BkDataContext BkDataContext
//        {
//            get
//            {
//                return _bkDataContext ?? (_bkDataContext = new BkDataContext());
//            }
//        }
//
//        public static Dictionary<Type, Type> ImplementationMapping
//        {
//            get
//            {
//                if (_implementationMapping == null)
//                {
//                    _implementationMapping = new Dictionary<Type, Type>();
//                    RegisterImplementationMappingItem<IBestExpensesRepository, BestExpensesRepository>();
//                    RegisterImplementationMappingItem<IImportStatusesRepository, ImportStatusesRepository>();
//                    RegisterImplementationMappingItem<IAccountsRepository, AccountsRepository>();
//                    RegisterImplementationMappingItem<IBestExpensesFilesRepository, BestExpensesFilesRepository>();
//                    RegisterImplementationMappingItem<ICategoriesRepository, CategoriesRepository>();
//                    RegisterImplementationMappingItem<ICurrenciesRepository, CurrenciesRepository>();
//                    RegisterImplementationMappingItem<ITargetsRepository, TargetsRepository>();
//                    RegisterImplementationMappingItem<IPaymentsRepository, PaymentsRepository>();
//                    RegisterImplementationMappingItem<IUsersRepository, UsersRepository>();
//                    RegisterImplementationMappingItem<IFinancistoExpensesRepository, FinancistoExpensesRepository>();
//                    RegisterImplementationMappingItem<IFinancistoFilesRepository, FinancistoFilesRepository>();
//                }
//
//                return _implementationMapping;
//            }
//        }
//
//        private static Dictionary<Type, IRepository> Repositories
//        {
//            get { return _repositories ?? (_repositories = new Dictionary<Type, IRepository>()); }
//        }
//        #endregion
//
//        #region Class Methods
//        public static TRepositoryInterface Get<TRepositoryInterface>() where TRepositoryInterface : IRepository
//        {
//            Type typeOfRepositoryInterface = typeof(TRepositoryInterface);
//
//            Type typeOfRepositoryImplementation = ImplementationMapping[typeOfRepositoryInterface];
//
//            IRepository repository = Repositories.ContainsKey(typeOfRepositoryImplementation) ? Repositories[typeOfRepositoryImplementation] : null;
//            
//            if (repository != null)
//            {
//                return (TRepositoryInterface)repository;
//            }
//
//            Repositories.Add(typeOfRepositoryImplementation, (IRepository)Activator.CreateInstance(typeOfRepositoryImplementation, BkDataContext));
//
//            return Get<TRepositoryInterface>();
//        }
//
//        public static void Release()
//        {
//            Repositories.Clear();
//            _repositories = null;
//
//            Release(ref _bkDataContext);
//        }
//
//        private static void RegisterImplementationMappingItem<TInterface, TRepository>() where TRepository : TInterface
//        {
//            ImplementationMapping.Add(typeof(TInterface), typeof(TRepository));
//        }
//
//        private static void Release<T>(ref T obj) where T : class 
//        {
//            if (obj != null)
//            {
//                if (obj is IDisposable)
//                {
//                    ((IDisposable) obj).Dispose();
//                }
//
//                obj = null;
//            }
//        }
//        #endregion
//    }
}